using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Text;
using System.Xml;
using TmxLib;

namespace TmxUtil
{
    public class TmxWriter
    {

        public static void Write(TmxImage image, TextWriter tmx)
        {
            XmlTextWriter writer = new XmlTextWriter(tmx);
            writer.Indentation = 2;
            writer.IndentChar = ' ';
            writer.Formatting = Formatting.Indented;

            writer.WriteStartDocument();
            writer.WriteStartElement("tmx");
            writer.WriteAttributeString("version", "1.1");

            writer.WriteStartElement("header");
            writer.WriteAttributeString("defaultLanguage", image.Header.DefaultCultureName);
            writer.WriteAttributeString("languages", image.Header.Cultures.AsXmlString);
            writer.WriteEndElement();

            writer.WriteStartElement("body");

            foreach (TmxItem item in image.Items)
            {
                    writer.WriteStartElement("tu");
                    {
                        writer.WriteStartElement("prop");
                        writer.WriteAttributeString("type", "context");
                        writer.WriteString(item.Context);
                        writer.WriteEndElement();

                        foreach(TmxItemValue value in item.Values)
                        {
                            if (value.Value != null)
                            {
                                writer.WriteStartElement("tuv");
                                writer.WriteAttributeString("lang", value.CultureName);
                                if (value.Ref != null)
                                    writer.WriteAttributeString("ref", value.Ref);

                                writer.WriteElementString("seg", value.Value);
                                writer.WriteEndElement();
                            }
                        }
                    }
                    writer.WriteEndElement();
            }

            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.Flush();
        }

        public static void Write(Solution solution, TextWriter tmx, TextWriter log)
        {
            XmlTextWriter writer = new XmlTextWriter(tmx);
            writer.Indentation = 2;
            writer.IndentChar = ' ';
            writer.Formatting = Formatting.Indented;

            writer.WriteStartDocument();
            writer.WriteStartElement("tmx");
            writer.WriteAttributeString("version", "1.1");

            writer.WriteStartElement("header");
            writer.WriteAttributeString("defaultLanguage", solution.DefaultCultureName);
            writer.WriteEndElement();

            writer.WriteStartElement("body");

            foreach(Project p in solution.Projects)
            {
                ResXItemCollection items2 = new ResXItemCollection(p.ResXList);
                ResXBrowser browser = new ResXBrowser(items2);
                foreach(string gk in browser.GlobalKeys)
                {
                    writer.WriteStartElement("tu");
                    {
                        writer.WriteStartElement("prop");
                        writer.WriteAttributeString("type", "context");
                        writer.WriteString(gk);
                        writer.WriteEndElement();

                        IEnumerable<ResXItem> localizedItems = browser.GetLocalizations(gk);
                        foreach(ResXItem item in localizedItems)
                        {
                            string culture = item.ResX.CultureName;
                            if (culture == string.Empty)
                                culture = solution.DefaultCultureName;

                            writer.WriteStartElement("tuv");
                            writer.WriteAttributeString("lang", culture);
                            string reference = item.ResX.Strings.GetReference(item.Key);
                            if (reference != null)
                                writer.WriteAttributeString("ref", reference);

                            writer.WriteElementString("seg", item.Value);
                            writer.WriteEndElement();
                        }
                    }
                    writer.WriteEndElement();
                }
            }

            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.Flush();
        }
    }
}
